package live.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

public class Format {

	Logger log = Log4jXmlConfig.getInstance().returnLogger(Format.class);
	static Format format=null;
	public synchronized static Format getInstance(){
		if(format==null){format=new Format();}
		return format;
	}
	
	public String dateFormatString(Date date, String format) {
		return new SimpleDateFormat(format).format(date);
	}

	public String returnFormatString(String timeStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.format(sdf.parse(timeStr));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.error("字符类型时间转换异常【" + timeStr + "】" + e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	public Date stringToDate(String timeStr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			return sdf.parse(timeStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			log.error("字符类型时间转换Date格式化异常【" + timeStr + "】" + e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	public String returnSystemNum() {
		Random rad = new Random();
		return dateFormatString(new Date(), "yyMMddHHmmss") + ""
				+ rad.nextInt(1000) + "";
	}

	public boolean isNotNullOrEmpty(Object aValue) {
		boolean lRtrnVal = true;
		if ((aValue == null) || (aValue.equals("")) || (aValue.equals("null"))) {
			lRtrnVal = false;
		}
		return lRtrnVal;
	}
	
	public boolean isNotNullOrZeroForList(List objList){
		boolean lRtrnVal = true;
		if(objList==null || objList.size()==0){
			lRtrnVal=false;
		}
		return lRtrnVal;
	}
	
	public boolean isNotNullOrZeroForMap(Map objMap){
		boolean lRtrnVal = true;
		if(objMap==null || objMap.size()==0){
			lRtrnVal=false;
		}
		return lRtrnVal;
	}

	// 产生随机的三位数
	public String getThree() {
		Random rad = new Random();
		return rad.nextInt(1000) + "";
	}
	
	/**
	 * @GB2312编码
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String encodeGB2312(String str)
			throws UnsupportedEncodingException {
		return URLEncoder.encode(str, "gb2312");
	}
	/**
	 * @UTF-8解码
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String decodeUTF(String str)
			throws UnsupportedEncodingException {
		return URLDecoder.decode(str, "UTF-8");
	}
	/**
	 * @UTF-8编码
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String encodeUTF(String str)
	throws UnsupportedEncodingException {
		return URLEncoder.encode(str, "UTF-8");
	}
	
	/**
	 * @GB2312解码
	 * @param str
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public String decoedGB2312(String str)
			throws UnsupportedEncodingException {
		try {
			str = str.replace(",", "%2c");
			str = str.replace(" ", "");
			str = str.replaceAll("%%", "%25%");
			if (str.substring(str.length() - 1, str.length()).equals("%")) {
				str = str.substring(0, str.length() - 1) + "%25";
			}
			return URLDecoder.decode(str, "gb2312");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.info("编码[" + str + "]解码时出现异常信息");
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 返回当前时间的下一天时间
	 * @param time
	 * @return
	 */
	public String returnNextDate(String time){
		Calendar calendarInDate = new GregorianCalendar();
		calendarInDate.setTime(stringToDate(time, "yyyy-MM-dd"));
		calendarInDate.add(calendarInDate.DATE, +1);
		return dateFormatString(calendarInDate.getTime(), "yyyy-MM-dd");
	}
}
